/**
 * @file       Context.cpp
 * @brief      TODO
 * @author     Huabo Zheng
 * @date       Jan 20, 2012
 * @Copyright  2012, www.netgear.com Inc. All rights reserved.
 */

#include "Context.h"
#include "typedefine.h"
#include "Panel.h"
#include <algorithm>
#include "ExectuteZone.h"

namespace NS_LCD_AFW
{

CMutex CContext::m_sMutex;

CContext::CContext()
{
}

CContext::~CContext()
{
}

void CContext::add(CPanel *pPanel)
{
	if (NULL == pPanel)
	{
		return;
	}

	CExectuteZone zone(&m_sMutex);
	CPanelPtrList::iterator iter = std::find(m_panelPtrList.begin(),
			m_panelPtrList.end(), pPanel);
	if (iter != m_panelPtrList.end())
	{
		return;
	}

	if (!m_panelPtrList.empty())
	{
		m_panelPtrList.back()->setVisible(false);
	}
	pPanel->setVisible(true);
	m_panelPtrList.push_back(pPanel);
}

CPanel *CContext::getActivePanel() const
{
	CExectuteZone zone(&m_sMutex);
	CPanel *pRet = NULL;
	if (!m_panelPtrList.empty())
	{
		pRet = m_panelPtrList.back();
	}
	return pRet;
}

void CContext::remove(CPanel *pPanel)
{
	CExectuteZone zone(&m_sMutex);
	CPanelPtrList::iterator iter = std::find(m_panelPtrList.begin(),
			m_panelPtrList.end(), pPanel);
	if (iter != m_panelPtrList.end())
	{
		pPanel->setVisible(false);
		m_panelPtrList.erase(iter);
		if (!m_panelPtrList.empty())
		{
			m_panelPtrList.back()->setVisible(true);
			m_panelPtrList.back()->show();
		}
	}
}

} /* namespace NS_LCD_AFW */
